package com.yzjs.localhost.service.sysRole.impl;

import cn.hutool.core.bean.BeanUtil;
import com.yzjs.common.base.service.BaseServiceImpl;
import com.yzjs.common.util.conversion.JsonUtil;
import com.yzjs.localhost.dto.ComparisonResultDTO;
import com.yzjs.localhost.dto.DataBundlingDTO;
import com.yzjs.localhost.dto.SysRoleEntityDTO;
import com.yzjs.localhost.dto.UserRightsDTO;
import com.yzjs.localhost.entity.sysRole.SysRoleEntity;
import com.yzjs.localhost.dao.sysRole.SysRoleMapper;
import com.yzjs.localhost.entity.sysUser.SysUserEntity;
import com.yzjs.localhost.entity.tDataPersistenceAfter.TDataPersistenceAfterEntity;
import com.yzjs.localhost.entity.tDataPersistenceBeforce.TDataPersistenceBeforceEntity;
import com.yzjs.localhost.entity.tNeusoftSysDept.TNeusoftSysDeptEntity;
import com.yzjs.localhost.entity.tNeusoftSysRole.TNeusoftSysRoleEntity;
import com.yzjs.localhost.entity.tNeusoftSysRoleMenu.TNeusoftSysRoleMenuEntity;
import com.yzjs.localhost.service.AnalysisService;
import com.yzjs.localhost.service.sysRole.SysRoleService;
import com.yzjs.localhost.service.tNeusoftSysRole.TNeusoftSysRoleService;
import com.yzjs.localhost.service.tNeusoftSysRoleMenu.TNeusoftSysRoleMenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;


@Service("sysRoleService")
public class SysRoleServiceImpl extends BaseServiceImpl<SysRoleEntity, SysRoleMapper> implements SysRoleService {

    @Autowired
    private TNeusoftSysRoleService neusoftSysRoleService;

    @Autowired
    private TNeusoftSysRoleMenuService neusoftSysRoleMenuService ;

    @Override
    public ComparisonResultDTO RoleCompliance(DataBundlingDTO dto, UserRightsDTO rights) throws Exception {
        TDataPersistenceBeforceEntity beforce = dto.getBeforce();
        String beforceInfo = beforce.getInfo();
        Map<String, Object> beforceParse = JsonUtil.json2map(beforceInfo);
        TDataPersistenceAfterEntity after = dto.getAfter();
        String afterInfo = after.getInfo();

        ComparisonResultDTO resultDTO = null;
        if (beforce.getOperatorCode().contains("role:add")) {
            Map<String, Object> afterParse = JsonUtil.json2map(afterInfo);
            // 比对 角色id,备注，角色编码，角色名称
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(), beforceParse, afterParse, "roleId,remark,roleKey,roleName");
        } else if (beforce.getOperatorCode().contains("role:edit")) {
            Map<String, Object> afterParse = JsonUtil.json2map(afterInfo);
            // 比对 角色id,备注，角色编码，角色名称
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(), beforceParse, afterParse, "roleId,remark,roleKey,roleName");
        } else if (beforce.getOperatorCode().contains("role:remove")) {
            beforceParse.put("delFlag", "2");
            Map<String, Object> afterParse = JsonUtil.json2map(afterInfo);
            // 比对 角色状态
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(), beforceParse, afterParse, "roleIds,delFlag");
            if (null == afterParse.get("roleIds")) {
                resultDTO.setAbstractName("主键ID为空");
                resultDTO.setWarningInfo("主键ID为空");
                resultDTO.setMathFlag(false);
                return resultDTO;
            }
            SysRoleEntity roleEntity = new SysRoleEntity();
            roleEntity.setRoleId(Long.valueOf((String) afterParse.get("roleIds")));
            roleEntity.setDelFlag((String) afterParse.get("delFlag"));
            roleEntity.setDataIdentification(after.getDataIdentification());
            this.insert(roleEntity);
            return resultDTO;
        }
        // 记录表表信息
        SysRoleEntityDTO roleDTO = JsonUtil.json2pojo(after.getInfo(), SysRoleEntityDTO.class);
        SysRoleEntity roleEntity = new SysRoleEntity();
        BeanUtil.copyProperties(roleDTO, roleEntity);
        roleEntity.setPermissionMirroring(JsonUtil.obj2jsonIgnoreNull(rights));
        roleEntity.setDataIdentification(after.getDataIdentification());
        this.insert(roleEntity);
        SynchronizationSysRole(roleEntity,beforce.getOperatorCode());
        return resultDTO;
    }

    @Override
    public void SynchronizationSysRole(SysRoleEntity roleEntity, String operatorCode) {
        TNeusoftSysRoleEntity neusoftSysRole  = new TNeusoftSysRoleEntity();
        neusoftSysRole.setRoleId(roleEntity.getRoleId());
        TNeusoftSysRoleEntity resultRole = neusoftSysRoleService.findOne(neusoftSysRole);

        if(resultRole == null){
            BeanUtils.copyProperties(roleEntity,neusoftSysRole);
            neusoftSysRoleService.insert(neusoftSysRole);
        } else {
            BeanUtils.copyProperties(roleEntity,resultRole);
            neusoftSysRoleService.update(resultRole);
        }
        // 更新角色菜单信息
        TNeusoftSysRoleMenuEntity  neusoftSysRoleMenu = new TNeusoftSysRoleMenuEntity();
        neusoftSysRoleMenu.setRoleId(roleEntity.getRoleId());
        neusoftSysRoleMenuService.delete(neusoftSysRoleMenu);


        List<Long> menuIds= roleEntity.getMenuIds();
        if (menuIds != null){
            List<TNeusoftSysRoleMenuEntity> roleMenus = new LinkedList<>();
            for(Long  x:menuIds ){
                TNeusoftSysRoleMenuEntity roleMenu  =new TNeusoftSysRoleMenuEntity();
                roleMenu.setRoleId(roleEntity.getRoleId());
                roleMenu.setMenuId(x);
                roleMenus.add(roleMenu);
            }
            neusoftSysRoleMenuService.insertBath(roleMenus);
        }

    }
}